/*
 * Bala.java
 *
 * Created on 23 de Abril de 2007, 20:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package labirinto;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author tschimuneck
 */
public class Bala extends Thread
{
    private int posX,posY;
    private int velX,velY;
    private JLabel lbBala;
    private Jogo jogo;
    
    /** Creates a new instance of Bala */
    public Bala(Jogo jogo)
    {
        this.jogo = jogo;
        this.lbBala = new JLabel();
        this.lbBala.setSize(10,10);
        this.lbBala.setOpaque(true);
        this.lbBala.setBackground(new Color(255,0,0));
        this.posX = this.posY = this.velX = this.velY = 0;
    }
    
    public void setPosX(int x)
    {
        this.posX = x;
    }
    
    public void setPosY(int y)
    {
        this.posY = y;
    }
    
    public void setVelX(int x)
    {
        this.velX = x;
    }
    
    public void setVelY(int y)
    {
        this.velY = y;
    }
    
    
    public int getPosX()
    {
        return this.posX;
    }
    
    public int getPosY()
    {
        return this.posY;
    }
    
    public int getVelX()
    {
        return this.velX;
    }
    
    public int getVelY()
    {
        return this.velY;
    }
    
    public void dispara()
    {
        this.jogo.getPaCenario().add(this.lbBala);
        this.lbBala.setLocation(this.posX,this.posY);
        if (this.velX != 0)
        {
            this.start();
        }
    }
    
    
    public void run()
    {
        Thing[][] mapa = this.jogo.getMapa();
        while(true)
        {
            if (this.posX < 640 - this.velX && this.posX > -Math.abs(this.velX))
            {
                int offX = (int)(this.posX+this.velX) / 32;
                int offY = (int)this.posY / 32;
                
                if (!mapa[offX][offY].getIsConcrete())
                {
                    Thing[][] mo = this.jogo.getMapaOponentes();
                    if (mo[offX][offY]!=null)
                    {
                        if (!mo[offX][offY].getIsOpponent())
                        {
                            this.posX += this.velX;
                            this.lbBala.setLocation(this.posX,this.posY);
                        }
                        else
                        {
                            mo[offX][offY].getOponente().morre();
                            this.jogo.getPaCenario().remove(this.lbBala);
                            this.jogo.getPaCenario().repaint();
                            break;
                        }
                    }
                    else
                    {
                        this.posX += this.velX;
                        this.lbBala.setLocation(this.posX,this.posY);
                    }
                }
                else
                {
                    this.jogo.getPaCenario().remove(this.lbBala);
                    this.jogo.getPaCenario().repaint();
                    break;
                }
            }
            else
            {
                this.jogo.getPaCenario().remove(this.lbBala);
                this.jogo.getPaCenario().repaint();
                break;
            }
            try
            {
                Thread.sleep(30);
            }
            catch(Exception e)
            {}
        }
    }
}
